package fpzhan.plane.program.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import fpzhan.plane.program.connect.RunFlow;
import fpzhan.plane.program.function.CodeBlockFunction;
import fpzhan.plane.program.manager.PlaneProgramManager;
import fpzhan.plane.program.test.school.*;
import fpzhan.plane.program.function.ConditionFunction;
import fpzhan.plane.program.param.Params;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProcessTest2 {

    public static void main(String[] args) throws Exception {
        PlaneProgramManager.useStruct("comment");

        Map map = new HashMap<>();
        AClass aClass = new AClass();
        aClass.setClassNo("一");
        aClass.setGrade("一");
        map.put("class",aClass);

        CodeBlockFunction initObject= param -> {
            param.put("classInfo",new ClassInfos());
            param.put("women",new ArrayList<>());
            param.put("men",new ArrayList<>());
        };

        CodeBlockFunction teacherByClass=param -> {
            List<Teacher> teachers = Query.teacherByClass(param.get("class",AClass.class));
            param.put("teachers",teachers);
        };

        CodeBlockFunction studentByTeacher=param -> {
            List<Student> students = Query.studentByTeacher(param.get("teacher",Teacher.class));
            param.put("students",students);
        };

        ConditionFunction isMan= param -> param.get("student",Student.class).isMan();

        CodeBlockFunction addManList=param -> param.get("men",List.class).add(param.get("student"));

        CodeBlockFunction addWomenList=param ->param.get("women",List.class).add(param.get("student"));

        CodeBlockFunction fullInfo=null;

        CodeBlockFunction addClasses=null;

        RunFlow runFlow = PlaneProgramManager.createFlow("student");
        runFlow.initParam(map).add("class").comment("初始化参数为，一年级一班信息")
            .flowBegin()
                .execute(setClasses).lose().add("classesInfo").comment("创建【班级信息集合】")
                .signBegin("queryTeacherAndStudent").comment("查询班级所有老师、所有学生信息,并放入【班级信息集合】")
                    .execute(initObject).addc("classInfo","women","men").comment("创建【班级基础信息】、【班级女生集合】、【班级男生集合】")
                    .execute(teacherByClass).addc("teachers").comment("查询【班级所有老师】")
                    .loop("teachers","teacher").comment("遍历获取每个老师，分管学生")
                        .execute(studentByTeacher).addc("students").comment("查询老师所有学生")
                        .loop("students","student").comment("遍历老师的所有学生,并放入不同性别的的学生集合中")
                            .branch().comment("判断性别")
                                .ifs(isMan).comment("如果是男生")
                                    .execute(addManList).none().comment("将当前学生信息放入【班级男生集合】")
                                .exitIfs()
                                .defaults().comment("如果是女生")
                                    .execute(addWomenList).none().comment("将当前学生信息放入【班级女生集合】")
                                .exitDefaults()
                            .branchEnd()
                        .loopEnd()
                    .loopEnd()
                    .execute(fullInfo).none().comment("将【班级老师】、【班级学生】、【班级男生】、【班级女生】、【班级基本信息】放入【班级信息】中")
                    .execute(addClasses).none().comment("将【班级信息】放入【班级信息集合】")
                .signEnd("queryTeacherAndStudent")
                .execute(Params.get("class",AClass.class).update(AClass::setGrade,"1").update(AClass::setClassNo,"2")).comment("将信息重置为一年级二班")
                .repeat("queryTeacherAndStudent").comment("将一年级二班所有信息放入【班级信息集合】")
            .flowEnd();
        System.out.println(JSON.toJSONString(runFlow.struct(), SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
    }

    private static CodeBlockFunction setClasses=param -> param.put("classesInfo",new ArrayList<>());
}
